﻿using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media;
using SimpleTool.Server;

namespace SimpleTool
{
    public sealed class OpenSetting
    {
        private readonly Serilog.ILogger Logger = Serilog.Log.ForContext<OpenSetting>();

        private OpenSetting() 
        {
            DBAddrenss = "云数据库";

            DBPassword = "云数据库";

            IsColud = true;

            OpenLog = "开";

            MainMenuStyle = "中文";
        }

        public static readonly OpenSetting option = new ();

        public readonly Dictionary<string, (string, string, LocalConfig.Path.FileType)> FieldKeyRelationMap = new ()
        {
            { nameof(IsColud), (nameof(LocalConfig.DBCacheTreeKey.IsCloud), LocalConfig.OwnerKey.DB, LocalConfig.Path.FileType.Nor) },
            { nameof(OpenLog), (nameof(LocalConfig.RuntimeLogKey.OpeningLogTextBox), LocalConfig.OwnerKey.Log, LocalConfig.Path.FileType.Nor) },
            { nameof(MainMenuStyle), (nameof(LocalConfig.BSCacheTreeKey.FuncIconStyle), LocalConfig.OwnerKey.BS, LocalConfig.Path.FileType.Nor) },
        };

        public void CutDB(string AddrenssName, string DBKey, 
            [CallerLineNumber] int Line = 0, 
            [CallerFilePath] string path = "", 
            [CallerMemberName] string name = "")
        {
            lock (this)
            {
                DBAddrenss = AddrenssName;

                DBPassword = DBKey;

                Logger.Print(RuntimeLog.LogType.Warning, $"{MethodBase.GetCurrentMethod()?.Name} {Line} {Path.GetFileName(path)} {name}");
            }
        }

        public void CutLog(string SwitchLog,
            [CallerLineNumber] int Line = 0,
            [CallerFilePath] string path = "",
            [CallerMemberName] string name = "")
        {
            lock (this)
            {
                OpenLog = SwitchLog;

                Logger.Print(RuntimeLog.LogType.Warning, $"{MethodBase.GetCurrentMethod()?.Name} {Line} {Path.GetFileName(path)} {name}");
            }
        }

        public void CutMenu(string MenuStyle,
            [CallerLineNumber] int Line = 0,
            [CallerFilePath] string path = "",
            [CallerMemberName] string name = "")
        {
            lock (this)
            {
                MainMenuStyle = MenuStyle;

                Logger.Print(RuntimeLog.LogType.Warning, $"{MethodBase.GetCurrentMethod()?.Name} {Line} {Path.GetFileName(path)} {name}");
            }
        }

        public void SetDBType(bool wIsColud,
            [CallerLineNumber] int Line = 0,
            [CallerFilePath] string path = "",
            [CallerMemberName] string name = "")
        {
            lock (this)
            {
                IsColud = wIsColud;

                Logger.Print(RuntimeLog.LogType.Warning, $"{MethodBase.GetCurrentMethod()?.Name} {Line} {Path.GetFileName(path)} {name}");
            }
        }

        public (string, string) DBPair([CallerLineNumber] int Line = 0,
                                       [CallerFilePath] string path = "",
                                       [CallerMemberName] string name = "")
        {
            lock (this)
            {
                Logger.Print(RuntimeLog.LogType.Warning, $"{MethodBase.GetCurrentMethod()?.Name} {Line} {Path.GetFileName(path)} {name}");

                return (DBAddrenss, DBPassword);
            }
        }

        public bool IsColudDB([CallerLineNumber] int Line = 0,
                              [CallerFilePath] string path = "",
                              [CallerMemberName] string name = "")
        {
            lock (this)
            {
                Logger.Print(RuntimeLog.LogType.Warning, $"{MethodBase.GetCurrentMethod()?.Name} {Line} {Path.GetFileName(path)} {name}");

                return IsColud;
            }
        }

        public string IsOpenLog([CallerLineNumber] int Line = 0,
                                [CallerFilePath] string path = "",
                                [CallerMemberName] string name = "")
        {
            lock (this)
            {
                Logger.Print(RuntimeLog.LogType.Warning, $"{MethodBase.GetCurrentMethod()?.Name} {Line} {Path.GetFileName(path)} {name}");

                return OpenLog;
            }
        }

        public string MenuStyle([CallerLineNumber] int Line = 0,
                                [CallerFilePath] string path = "",
                                [CallerMemberName] string name = "")
        {
            lock (this)
            {
                Logger.Print(RuntimeLog.LogType.Warning, $"{MethodBase.GetCurrentMethod()?.Name} {Line} {Path.GetFileName(path)} {name}");

                return MainMenuStyle;
            }
        }

        private string DBAddrenss { set; get; }

        private string DBPassword { set; get; }

        private bool IsColud { set; get; }

        private string OpenLog { set; get; }

        private string MainMenuStyle { set; get; }
    }

    public class MainFuncListItem
    {
        public string Content { set; get; } = string.Empty;

        public FontFamily Family { set; get; } = new FontFamily("YaHei");

        public double Fontsize { set; get; } = 29;

        public string? TipHint { set; get; } = null;

        public Brush ForeGround { set; get; } = "DeepMatrix".FindSolidBrush();

        public FontWeight FontWeight { set; get; } = FontWeights.Medium;

        public (string, string) Warehouse { set; get; } = (string.Empty, string.Empty);
    };

    public class MainSectionModel
    {
        public FuncType StoryMMenuSelectItem = FuncType.Invalid;

        public enum MMenuSType
        {
            chz,
            icon
        }

        public enum FuncType
        {
            Invalid,
            Global,
            Serial,
            Capacity,
            Activity
        }

        public enum FuncItemKey
        {
            //  Global 类型
            GlobalConfig,
            GlobalSerial,
            GlobalCapacityTest,
            GlobalProTest,
            GlobalMenu,

            //  Serial 类型  
            SerialBasicConfig,
            SerialCommand
        }

        public Dictionary<string, string> PublicSettingTranslateMap { get; } = new ()
        {
            { "true", "开" },
            { "false", "关" },
            { nameof(MMenuSType.chz), "中文" },
            { nameof(MMenuSType.icon), "图标" },
        };

        public Dictionary<FuncItemKey, MainFuncListItem> ListItemOptionMap { get; } = new()
        {
            //  Global 项目
            [FuncItemKey.GlobalConfig] = new MainFuncListItem
            {
                FontWeight = FontWeights.UltraLight,
                Warehouse = ("\ue625", "配置")
            },
            [FuncItemKey.GlobalSerial] = new MainFuncListItem
            {
                FontWeight = FontWeights.Light,
                Warehouse = ("\ue633", "串口工具")
            },
            [FuncItemKey.GlobalCapacityTest] = new MainFuncListItem
            {
                FontWeight = FontWeights.Light,
                Warehouse = ("\ue687", "测量工具")
            },
            [FuncItemKey.GlobalProTest] = new MainFuncListItem
            {
                FontWeight = FontWeights.Light,
                Warehouse = ("\ue64c", "产测工具")
            },

            //  Serial 项目
            [FuncItemKey.GlobalMenu] = new MainFuncListItem
            {
                FontWeight = FontWeights.Light,
                Warehouse = ("\ue609", "菜单")
            },

            [FuncItemKey.SerialBasicConfig] = new MainFuncListItem
            {
                FontWeight = FontWeights.Light,
                Warehouse = ("\ue661", "串口配置")
            },
            [FuncItemKey.SerialCommand] = new MainFuncListItem
            {
                FontWeight = FontWeights.Light,
                Warehouse = ("\ue614", "串口指令")
            }
        };

        private MainFuncListItem GetListOption(FuncItemKey key, bool isChz = false)
        {
            MainFuncListItem item = ListItemOptionMap[key];

            if (isChz)
            {
                item.Content = item.Warehouse.Item2;
                item.Family = "YaHei".FindSource<FontFamily>() ?? new FontFamily("KaiTi");
                item.Fontsize = 17;
                item.TipHint = null;
                item.ForeGround = "GaoguiZong".FindSolidBrush();
            }
            else
            {
                item.Content = item.Warehouse.Item1;
                item.TipHint = item.Warehouse.Item2;
                item.Fontsize = 32;
                item.Family = "Icon".FindSource<FontFamily>() ?? new FontFamily("KaiTi");
                item.ForeGround = "HongTao".FindSolidBrush();
            }
            return item;
        }

        public IEnumerable<MainFuncListItem> GetFuncItems(FuncType type, bool isChz = false)
        {
            FuncItemKey[] itemKeys = type switch
            {
                FuncType.Global =>
                [
                    FuncItemKey.GlobalConfig,
                    FuncItemKey.GlobalSerial,
                    FuncItemKey.GlobalCapacityTest,
                    FuncItemKey.GlobalProTest
                ],
                FuncType.Serial =>
                [
                    FuncItemKey.GlobalMenu,
                    FuncItemKey.GlobalConfig,
                    FuncItemKey.SerialBasicConfig,
                    FuncItemKey.SerialCommand
                ],
                FuncType.Activity =>
                [
                    FuncItemKey.GlobalMenu,
                    FuncItemKey.GlobalConfig
                ],
                FuncType.Capacity =>
                [
                    FuncItemKey.GlobalMenu
                ],
                _ => []
            };

            return itemKeys.Select(key => GetListOption(key, isChz));
        }
    }
}
